﻿package com.sos.ui
{ 
    import flash.display.MovieClip; 
     
    public class CardSelectionOverlay extends MovieClip 
    { 
	
		public var cardList:Array = new Array();
		public var cardCount:Number = 0;
		private var topRightCornerX:Number;
		private var topRightCornerY:Number;
		private var cardsXLine:Number;
		
		public function CardSelectionOverlay() {
			//trace('INIT CARD HOLDER ' + this.name);
			this.x = 403.20;
			this.y = 288;
			topRightCornerX = -290;
			topRightCornerY = -150;
			cardsXLine = 10;
			
        } 
		
		public function addCard(ac:ActionCardClip):void {
			
			cardList[ac.instanceId] = ac;
			this.addChild(ac);
			
			cardCount++;
			ac.cardOrder = cardCount;
			
			ac.x = getX(ac.cardOrder);
			ac.y = getY(ac.cardOrder);
			//trace(cardCount);
			
		}
		
		public function getX(cardOrder:Number):Number {
			var cardX:Number = topRightCornerX + (cardOrder - (getLine(cardOrder) - 1)*cardsXLine  - 1)*65;
			return cardX;
		}
		
		public function getY(cardOrder:Number):Number {
			var cardY:Number = topRightCornerY + (getLine(cardOrder) - 1)*75;
			return cardY;
		}
		
		public function getLine(cardOrder:Number):Number {
			
			var cardLine = Math.ceil(cardOrder/cardsXLine);
			//trace("CARD ", cardOrder, " LINE IS ", cardLine);
			return cardLine;
		}
		
		public function clearOverlay():void {
			for each (var ac:ActionCardClip in cardList) {
				//trace("clearing", ac.name,"from overlay");
				ac.sendToGrave();
				cardCount--;
				//cardList.splice(ac.instanceId,1);
				//removeCard(ac.instanceId);
				
			}
			cardList = new Array();
		}
		
		public function removeCard(cardId:String):void {
			
			//this.removeChild(cardList[cardId]);
			for each (var ac:ActionCardClip in cardList) {
				if ((ac.instanceId != cardId) && (ac.cardOrder > cardList["c_" + cardId].cardOrder)) {
					
					ac.cardOrder--;
					ac.x = getX(ac.cardOrder);
					ac.y = getY(ac.cardOrder);
				}
			}
			cardList.splice(cardId,1);
			cardCount--;
			
		}
		
		
    } 
}